package com.tgy.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tgy.admin.Service.CategoryService;
import com.tgy.page.PageQuery;
import com.tgy.pojo.BlogCategory;
import com.tgy.result.GraceJSONResult;
import com.tgy.result.LayuiTableResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author tianguiyin
 * @version 1.0
 * @date 2022/6/21 21:01
 */
@Validated
@Api(value = "文章分类控制器", tags = {"文章分类管理"})
@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @ApiOperation("获取文章分类列表")
    @SaCheckPermission("blogcategory:query")
    @GetMapping("getCategoryList")
    public LayuiTableResult getCategoryList(BlogCategory bo, PageQuery pageQuery){
        return categoryService.getBlogCategoryList(bo,pageQuery);
    }

    @ApiOperation("添加文章分类")
    @SaCheckPermission("blogcategory:add")
    @PostMapping("addBlogCategory")
    public GraceJSONResult addBlogCategory(BlogCategory BlogCategory){
        return categoryService.addBlogCategory(BlogCategory);
    }

    @ApiOperation("修改文章分类")
    @SaCheckPermission("blogcategory:update")
    @PostMapping("updateBlogCategory")
    public GraceJSONResult updateBlogCategory(BlogCategory BlogCategory){
        return categoryService.updateBlogCategory(BlogCategory);
    }

    @ApiOperation("删除文章分类")
    @SaCheckPermission("blogcategory:del")
    @PostMapping("delBlogCategory")
    // @PostMapping("delBlogCategory/{id}")
    public GraceJSONResult delBlogCategory(@ApiParam("ID必须传入") String id){
        return categoryService.delBlogCategory(id);
    }

    @ApiOperation("批量删除文章分类")
    @SaCheckPermission("blogcategory:del")
    @PostMapping("delBlogCategoryIds")
    public GraceJSONResult delBlogCategoryIds(@ApiParam("ids必须传入") String ids){
        List<String> list = new ArrayList<>(Arrays.asList(ids.split(",")));
        return categoryService.delBlogCategoryIds(list);
    }

    @ApiOperation("根据ID查询文章分类")
    @SaCheckPermission
    @PostMapping("getBlogCategoryById")
    public GraceJSONResult getBlogCategoryById(@ApiParam("ID必须传入") String id){
        return categoryService.getBlogCategoryById(id);
    }


    @ApiOperation("根据ID查询文章分类")
    @SaCheckPermission
    @GetMapping("getCategoryListNotPage")
    public GraceJSONResult getCategoryList(String articleId){
        return GraceJSONResult.ok(categoryService.getCategoryList(articleId));
    }


}
